package org.waddy.framework.pub.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;



import org.apache.log4j.Logger;
import org.waddy.framework.app.exception.AppException;
import org.waddy.framework.app.exception.SysException;
import org.waddy.framework.pub.dao.ISysDateDAO;



/**
 * 
 * Title: 资源管理系统<br>
 * Description: <br>
 * Date: 2012-3-22 <br>
 * Copyright (c) 2012 CATTSoft <br>
 *
 * @author yanxin
 */
public class SysDate {

    private static Logger log = Logger.getLogger(SysDate.class);
    
    private static ISysDateDAO sysDateDAO;
    
	public ISysDateDAO getSysDateDAO() {
		return sysDateDAO;
	}

	public void setSysDateDAO(ISysDateDAO sysDateDAO) {
		SysDate.sysDateDAO = sysDateDAO;
	}

	private static long timeInterval = -1; // 数据库时间和应用服务器时间差
    
    private static SysDate instance ;
    
    public SysDate() throws AppException,SysException {
    	
	}

    public static SysDate getInstance() throws AppException, SysException {
		if (instance == null) {
			instance = new SysDate();
		}
		return instance;
	}

   
    /**
     * 获取当前日期
     * 
     * @return
     * @throws SysException 
     * @throws AppException 
     */
    public java.sql.Date getCurrentDbDate() throws AppException, SysException {
    	try {
            //java.util.Date dbTime = getCurrentOracleSysDate();
            java.util.Date dbTime = getCurrentMysqlSysDate();
            java.util.Date serverTime = new java.util.Date();
            timeInterval = dbTime.getTime() - serverTime.getTime();
        } catch (SysException e) {
            log.debug(e.getMessage());
            timeInterval = -1;
            throw e;
        }catch (AppException e) {
            log.debug(e.getMessage());
            timeInterval = -1;
            throw e;
        }
        return new java.sql.Date(new java.util.Date().getTime() + SysDate.getInstance().getTimeInterval());
    }
    
    

    /**
     * 获取当前日期
     * 
     * @return
     * @throws SysException 
     * @throws AppException 
     */
    public static java.sql.Date getCurrentDate() throws AppException, SysException {
        return SysDate.getInstance().getCurrentDbDate();
    }


    /**
     * 取得系统当前时间的年
     * 
     * @return String
     * @throws SysException 
     * @throws AppException 
     */
    public static String getSysDateYear() throws AppException, SysException {
        Date date = SysDate.getInstance().getCurrentDbDate();
        return getSysDateYear(date);
    }

    /**
     * 取得指定时间的年
     * 
     * @param date
     * @return String
     */
    public static String getSysDateYear(Date date) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        String year = String.valueOf(cd.get(Calendar.YEAR));
        return year;
    }

    /**
     * 取得系统当前时间的月
     * 
     * @return String
     * @throws SysException 
     * @throws AppException 
     */

    public static String getSysDateMonth() throws AppException, SysException {
        Date date = SysDate.getInstance().getCurrentDbDate();
        return getSysDateMonth(date);
    }

    /**
     * 取得指定时间的月
     * 
     * @param date
     * @return String
     */
    public static String getSysDateMonth(Date date) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        String month = String.valueOf(cd.get(Calendar.MONTH) + 1);
        if (month.length() == 1)
            month = "0" + month;
        return month;
    }

    private static java.util.Date getCurrentOracleSysDate() throws SysException, AppException {
        return sysDateDAO.getCurrentOracleSysDate();
    }
    
    private static java.util.Date getCurrentMysqlSysDate() throws SysException, AppException {
    	return sysDateDAO.getCurrentMysqlSysDate();
    }

    /**
     * 
     * @return
     */
	public static long getTimeInterval() {
		return timeInterval;
	}

	
	public static void setTimeInterval(long timeInterval) {
		SysDate.timeInterval = timeInterval;
	}

	/**
     * 根据时间字符串得到时间Date，如"2004-01-02 4:45:34"可以转换成同样日期的Date
     * @param dateStr String
     * @throws SysDateException
     * @return Date
     * @throws SysException 
     */
    public static Date getSysDateTime(String dateTimeStr) throws SysException{
        if (dateTimeStr == null||dateTimeStr.trim().length()==0)return null;
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
			return sd.parse(dateTimeStr);
		} catch (ParseException e) {
			 throw new SysException("10001", e);
		}
    }
    
    /**
     * 根据时间字符串得到时间Timestamp，如"2004-01-02 4:45:34"可以转换成同样日期的Date
     * @param dateStr String
     * @throws SysDateException
     * @return Date
     * @throws SysException 
     */
    public static java.sql.Timestamp getTimestamp(String timeStr) throws SysException{
        return new java.sql.Timestamp(getSysDateTime(timeStr).getTime());
    }
}
